import vue from '@vitejs/plugin-vue'
import path from 'path'
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
import viteCompression from 'vite-plugin-compression';
import {visualizer} from "rollup-plugin-visualizer";
import {createHtmlPlugin} from 'vite-plugin-html'
import {cdnCss, cdnJs, externals} from './cdn'

export default () => {
    return {
        build: {
            // 打包环境移除console.log，debugger
            terserOptions: {
                compress: {
                    drop_console: true,
                    drop_debugger: true
                }
            },
            rollupOptions: {
                external: Object.keys(externals),
                output: {
                    globals: externals,
                },
            },
        },
        plugins: [
            vue(),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')], // Specify the icon folder to be cached
                symbolId: 'icon-[dir]-[name]', // Specify symbolId format
            }),
            viteCompression({
                algorithm: "gzip", // 指定压缩算法为gzip,[ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
                ext: ".gz", // 指定压缩后的文件扩展名为.gz
                threshold: 10240, // 仅对文件大小大于threshold的文件进行压缩，默认为10KB
                deleteOriginFile: false, // 是否删除原始文件，默认为false
                compressionOptions: {level: 9}, // 指定gzip压缩级别，默认为9（最高级别）
                verbose: true, //是否在控制台输出压缩结果
                disable: false, //是否禁用插件
            }),
            visualizer({
                open: false,
                gzipSize: true,
                filename: "stats.html", //分析图生成的文件名
                brotliSize: true
            }),
            createHtmlPlugin({
                inject: {
                    data: {
                        injectCss: cdnCss,
                        injectJs: cdnJs,
                    },
                },
            }),
        ],
        base: './',
        resolve: {
            alias: {
                '@': path.resolve('./src'), // 相对路径别名配置，使用 @ 代替 src
            },
        },
        css: {
            preprocessorOptions: {
                scss: {
                    javascriptEnabled: true,
                },
            },
        },

        //代理跨域
        server: {
            headers: {
                'Cross-Origin-Opener-Policy': 'unsafe-none',
                'Cross-Origin-Embedder-Policy': 'unsafe-none',
            },
            proxy: {
                '/api': {
                    target: "https://back.xuxiao888.com",
                    changeOrigin: true,
                    rewrite: (path: string) => path.replace(/^\/api/, ''),
                },
            },
        },
    }
}
